package com.ruoyi.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.Product;
import com.ruoyi.web.domain.RefundRecord;
import com.ruoyi.web.service.IRefundRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 退款Controller
 * 
 * @author song
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/admin/record")
public class RefundRecordController extends BaseController
{
    @Autowired
    private IRefundRecordService refundRecordService;

    /**
     * 查询退款列表
     */
    @PreAuthorize("@ss.hasPermi('admin:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(RefundRecord refundRecord)
    {
        startPage();
        List<RefundRecord> list = refundRecordService.selectRefundRecordList(refundRecord);
        return getDataTable(list);
    }

    /**
     * 导出退款列表
     */
    @PreAuthorize("@ss.hasPermi('admin:record:export')")
    @Log(title = "退款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RefundRecord refundRecord)
    {
        List<RefundRecord> list = refundRecordService.selectRefundRecordList(refundRecord);
        ExcelUtil<RefundRecord> util = new ExcelUtil<RefundRecord>(RefundRecord.class);
        util.exportExcel(response, list, "退款数据");
    }

    /**
     * 获取退款详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(refundRecordService.selectRefundRecordById(id));
    }

    /**
     * 新增退款
     */
    @PreAuthorize("@ss.hasPermi('admin:record:add')")
    @Log(title = "退款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RefundRecord refundRecord)
    {
        return toAjax(refundRecordService.insertRefundRecord(refundRecord));
    }

    /**
     * 修改退款
     */
    @PreAuthorize("@ss.hasPermi('admin:record:edit')")
    @Log(title = "退款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RefundRecord refundRecord)
    {
        return toAjax(refundRecordService.updateRefundRecord(refundRecord));
    }

    /**
     * 删除退款
     */
    @PreAuthorize("@ss.hasPermi('admin:record:remove')")
    @Log(title = "退款", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(refundRecordService.deleteRefundRecordByIds(ids));
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RefundRecord> util = new ExcelUtil<>(RefundRecord.class);
        List<RefundRecord> refundRecords = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = refundRecordService.importData(refundRecords, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<RefundRecord> util = new ExcelUtil<>(RefundRecord.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
